<?php
namespace Admin\Controller;

use Common\Model\CompanyListModel;
use Common\Model\TreeModel;
use Common\Model\User\UserViewLogModel;
use Common\Model\UserCardListModel;
use Common\Util\BaseData;
use Common\Util\UserData;
use Services\Upload\OssFileService;

class CardController extends AdminController{

	public function index(){
		$this->meta_title = '名片管理';
		$card_m = new UserCardListModel();
		$where_map = [];
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$where_map['g.c_cn_true_name|g.c_phone'] = ['eq', $_GET['name']];
		}
		$page = I('page', 1, 'intval'); // 默认显示第一页数据
		$row = 20;
		$data = $card_m
			->alias('g')
			->where($where_map)
			->order("g.id desc")
			->page($page, $row)
			->select();
		foreach($data as $key => $val){
			$data[$key]['zhiye_label'] = explode(',', $val['zhiye_label']);
			$data[$key]['c_touxiang'] = M("company_list")->where(['id' => $val['c_company_id']])->getField("company_logo");
			$data[$key]['add_time'] = date("Y-m-d H:i:s", $val['add_time']);
			$data[$key]['square_is_show_title'] = $val['square_is_show'] == 0 ? '否' : '<span style="color:red;">是</span>';
			$data[$key]['card_status_name'] = $val['card_status'] == 1 ? '正常' : '禁止';
			$data[$key]['c_weixin_img'] = OssFileService::getOssUrl($val['c_weixin_img']);
			$allViewPerson = M("user_view_log")->where([
				'type' => UserViewLogModel::TYPE_CARD,
				'model_id' => $val['id']
			])->group("user_id")->select();
			$data[$key]['all_view_person'] = count($allViewPerson);
		}
		$count = $card_m
			->alias('g')
			->where($where_map)
			->count();
		$list_data ['list_data'] = $data;
		// 分页
		if($count > $row){
			$page = new \Think\Page ($count, $row);
			$page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$list_data ['_page'] = $page->show();
		}
		$company_list = M("company_list")->select();
		$this->assign("company_list", $company_list);
		$this->assign('all_count', $count);
		$this->assign('list_data', $list_data);
		$this->assign('getting', $_GET);
		$this->display();
	}

	public function edit(){
		$card_m = new UserCardListModel();
		$id = I('get.id', 0);
		if(!$info = $card_m->getInfo(['id' => $id])){
			$this->error('对不起，信息不存在');
		}
		$info['zhiye_label'] = explode(',', $info['zhiye_label']);
		$info['square_is_show_title'] = $info['square_is_show'] == 1 ? '否' : '是';
		$this->assign("info", $info);
		$this->meta_title = '名片详情';
		$this->display('edit');
	}

	public function dataImport(){
		$map = [];
		if(isset($_GET['name']) && !empty($_GET['name'])){
			$map['g.c_cn_true_name|g.c_phone'] = ['like', "%$_GET[name]%"];
		}
		$card_m = new UserCardListModel();
		$data = $card_m
			->alias('g')
			->where($map)
			->order("g.is_order asc,g.id desc")
			->select();

		$BasePath = SITE_PATH."/Application/Common";
		require_once($BasePath.'/PHPExcel/Classes/PHPExcel.php');
		$objPHPExcel = new \PHPExcel();
		$obj_Sheet = $objPHPExcel->getActiveSheet(); //获得当前活动sheet的活动对象
		$objPHPExcel->getActiveSheet()->getColumnDimension(\PHPExcel_Cell::stringFromColumnIndex(0))->setAutoSize(true);
		// 设置水平居中
		$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getAlignment()->setHorizontal
		(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		// 设置垂直居中
		$objPHPExcel->getActiveSheet()->getStyle('A1:I1')->getAlignment()->setVertical
		(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
		//设置单元格宽度
		$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(100); //设置默认行高
		$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);    //第一行行高
		//设置单元格宽度
		$app_setting = BaseData::getAppSetting();
		$objPHPExcel->getActiveSheet()
			->setCellValue('A1', '姓名')
			->setCellValue('B1', '联系电话')
			->setCellValue('C1', '微信号')
			->setCellValue('D1', '标签')
			->setCellValue('E1', '地址')
			->setCellValue('F1', '广场展示')
			->setCellValue('G1', '名片展示')
			->setCellValue('H1', '主页访问')
			->setCellValue('I1', '添加时间');
		$b_key = 1;
		foreach($data as $key => $val){
			$val['square_is_show_title'] = $val['square_is_show'] == 1 ? '否' : '是';
			$val['card_status_name'] = $val['card_status'] == 1 ? '正常' : '禁止';
			$zhuye_view = M("user_view_log")->where(['view_card_id' => $val['id']])->group("user_id")->select();
			$val['all_view_person'] = count($zhuye_view);
			$b_key++;
			//设置循环从第二行开始
			$objPHPExcel->getActiveSheet()
				//Excel的第A列，name是你查出数组的键值字段，下面以此类推
				->setCellValue('A'.$b_key, $val['c_cn_true_name'])
				->setCellValue('B'.$b_key, $val['c_phone'])
				->setCellValue('C'.$b_key, $val['c_weixin'])
				->setCellValue('D'.$b_key, $val['zhiye_label'])
				->setCellValue('E'.$b_key, $val['c_address'])
				->setCellValue('F'.$b_key, $val['square_is_show_title'])
				->setCellValue('G'.$b_key, $val['card_status_name'])
				->setCellValue('H'.$b_key, $val['all_view_person'].'人')
				->setCellValue('I'.$b_key, date("Y-m-d H:i:s", $val['add_time']));
			$objDrawing = new \PHPExcel_Worksheet_Drawing();//图片生成
			//设置宽度高度
			$objDrawing->setHeight(100);//照片高度
			$objDrawing->setWidth(100); //照片宽度

			// /*设置图片要插入的单元格*/
			$objDrawing->setCoordinates('I'.$b_key);
			// 图片偏移距离
			$objDrawing->setOffsetX(0);
			$objDrawing->setOffsetY(0);
			$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
		}

		$obj_Sheet->setTitle("世纪同昌名片");//设置当前活动Sheet名称
		//设置当前的表格
		ob_end_clean();
		header('Content-Type: application/vnd.ms-excel; charset=utf-8'); //文件类型
		header('Content-Disposition: attachment;filename=card'.date('Ymd').'.xls'); //文件名
		header('Cache-Control: max-age=0');
		$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel 2003
		$objWriter->save('php://output');
	}

	public function setStatus(){
		$field = I('get.field');
		$card_status = I('get.status');
		$aid = I('get.aid');
		$t_user_card_list = M("user_card_list");
		$data['id'] = $aid;
		$data[$field] = $card_status;
		$data['update_time'] = time();
		if($t_user_card_list->save($data)){
			$this->return_json_data(1, '更新成功');
		}else{
			$this->return_json_data(0, '更新失败');
		}
	}

	/**
	 * 统一ajax数据返回
	 *
	 * @param $code
	 * @param $info
	 */
	public function return_app_data($code, $info){
		echo json_encode(['code' => $code, 'info' => $info]);
		exit();
	}
}
